<template>
	<view class="index-page">
		<view style="height: 8px"></view>
		<u-list v-if="dataSource.length > 0">
			<u-list-item v-for="(item, index) in dataSource" :key="index">
				<u-cell :title="item.DirectoryName" @click="handleToList(item)">
					<u-avatar :text="item.DirectoryName.substring(0, 1)" fontSize="16" slot="icon" shape="square" randomBgColor></u-avatar>
				</u-cell>
			</u-list-item>
		</u-list>
		<Empty v-else />
	</view>
</template>

<script>
import request from '@/utils/request.js';
import { encrypt, isMobile } from '@/utils/utils.js';
import Empty from '@/components/empty.vue';
import globalConfig from '@/utils/globalConfig.js';

import VConsole from 'vconsole';

export default {
	components: {
		Empty
	},
	data() {
		return {
			dataSource: []
		};
	},
	created() {
		if (globalConfig.DEBUGGER) {
			new VConsole();
		}
		if (!isMobile()) {
			window.location.href = globalConfig.PC_URL;
		}
	},
	onLoad() {
		this.getUserId();
	},

	onPullDownRefresh() {
		let params = encrypt(JSON.parse(localStorage.getItem('FIXED_PARAMS')));
		this.getList('enablePullDownRefresh', params);
	},
	methods: {
		// 获取userid
		getUserId() {
			this.showToast();
			const that = this;
			// 是移动端，并且没有本地存储的时候需要拿code去请求接口
			let url = new URL(window.location.href);
			let params = new URLSearchParams(url.search);
			const code = params.get('code');
			request(`/WeCom/EnterpriseWeChatIdentityRecognition?code=${code}`, 'POST')
				.then((res) => {
					this.hideToast();
					console.log('请求成功:', res);
					if (res && res.code === 0) {
						// success
						let FIXED_PARAMS = {
							EnterpriseWeChatID: res.WeChatUserID
						};
						localStorage.setItem('FIXED_PARAMS', JSON.stringify(FIXED_PARAMS));
						let datas = encrypt(JSON.parse(localStorage.getItem('FIXED_PARAMS')));
						that.getList(null, datas);
					} else {
						uni.showToast({
							title: '暂无权限',
							duration: 2000,
							icon: 'none',
							class: 'my-toast'
						});
					}
				})
				.catch((err) => {
					console.error('请求失败:', err);
				});
		},

		// 获取列表
		getList(isLoading, params) {
			if (!isLoading) {
				this.showToast();
			}
			console.log('获取列表参数', params);
			// 发起请求示例
			request('/EnterpriseWeChatIMSSystemWorkbench/GetDirectoryApi', 'POST', { ...params })
				.then((res) => {
					console.log('请求成功:', res);
					let data = JSON.parse(res.Data);
					console.log(data);
					if (data) {
						this.hideToast();
						if (data.length > 0) {
							this.dataSource = data;
						} else {
							this.dataSource = [];
						}
					}
					if (isLoading) {
						uni.stopPullDownRefresh();
					}
				})
				.catch((err) => {
					console.error('请求失败:', err);
				});
		},

		// 去项目列表
		handleToList(item) {
			console.log(item);
			uni.navigateTo({
				url: `/pages/list/index?EntityName=${item.EntityName}`
			});
		},
		showToast() {
			uni.showLoading({
				title: '正在加载',
				class: 'my-toast'
			});
		},
		hideToast() {
			uni.hideLoading();
		}
	}
};
</script>

<style lang="scss">
::v-deep {
	.u-cell .u-line {
		display: none;
	}
	.u-cell__title-text {
		margin-left: 12rpx;
		font-size: 30rpx;
	}
	.u-avatar {
		width: 60rpx !important;
		height: 60rpx !important;
	}
	.u-text .u-text__value {
		font-size: 28rpx !important;
	}
}
</style>
